using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TaugeLibrary.Classes
{
    public class IniFile
    {
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
            int size, string filePath);

        public IniFile(string INIPath)
        {
            path = INIPath;
        }

        public void writeString(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        public string readString(string Section, string Key, string def)
        {

            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, def, temp,
                                            255, this.path);
            //if (i <= 0)
            //    return def;
            //else
            return temp.ToString();


        }

        public int readInt(string Section, string Key, int def)
        {
            try
            {
                return Convert.ToInt32(readString(Section, Key, def.ToString()));
            }
            catch
            {
                return def;
            }
        }

        public bool readBoolean(string Section, string Key, bool def)
        {
            try
            {
                return Convert.ToBoolean(readString(Section, Key, def.ToString()));
            }
            catch
            {
                return def;
            }
        }

    }
}
